package com.qf.service.impl;

import com.qf.dao.AddressDao;
import com.qf.dao.OrderDao;
import com.qf.dao.impl.AddressDaoImpl;
import com.qf.dao.impl.OrderDaoImpl;
import com.qf.domain.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.GoodsService;
import com.qf.service.OrderService;
import com.qf.utils.DataSourceUtil;

import java.sql.SQLException;
import java.util.List;

public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    AddressDao addressDao=new AddressDaoImpl();
    @Override
    public void submitOrder(Order order, List<Cart> cartList) {
        try {
            //事务处理
            //手动开启事务处理
            DataSourceUtil.begin();
            //1向订单表中添加订单
            orderDao.add(order);
            //2向订单详情中添加数据***
            for (Cart cart : cartList) {
                OrderDetail orderDetail = new OrderDetail(0,order.getId(),cart.getPid(),cart.getNum(),cart.getMoney());
                orderDao.addDetail(orderDetail);
            }
            //3清空购物车
            CartService cartService = new CartServiceImpl();
            cartService.clear(order.getUid());//order 关联用户表和地址表   order_detail是order 和商品表， cart表有商品
            //事务提交
            DataSourceUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                //手动处理事务，事务回滚
                DataSourceUtil.rollback();//有任何问题都事务回滚
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            throw new RuntimeException(e);
        }finally {
            try {
                //手动处理事务，释放资源
                DataSourceUtil.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Order> find(Integer uid) {
        List<Order> orderList = orderDao.select(uid);//遍历订单表的数据，找到订单表的所有aid，把aid对应的地址再存到addlist，让前端获取去
        if (orderList != null && orderList.size()>0) {
            //获取地址
            AddressService addressService = new AddressServiceImpl();
            List<Address> addressList = addressService.find(uid);//得到uid 用户的所有地址的列表
            if (addressList != null) {
                for (Order order : orderList) {
                    for (Address address : addressList) {
                        //订单表的aid字段与地址表的id匹配获取
                        if(order.getAid()==address.getId()){
                            order.setAddress(address);
                        }
                    }
                }
            }


        }

        return orderList;
    }

    @Override
    public Order findByOid(String oid) {
        Order order = orderDao.selectByOid(oid);
        if (order != null) {
            //手动封装地址信息
            AddressService addressService = new AddressServiceImpl();
            List<Address> addressList = addressService.find(order.getUid());
            if (addressList != null) {
                for (Address address : addressList) {
                    if (order.getAid()==address.getId()){
                        order.setAddress(address);
                    }
                }
            }
        }
        return order;
    }

    @Override
    public List<OrderDetail> findDetail(String oid) {
        List<OrderDetail> orderDetails = orderDao.selectDetail(oid);
        if (orderDetails != null && orderDetails.size()>=0) {
            //手动封装商品表  item.goods.name
            GoodsService goodsService = new GoodsServiceImpl();
            for (OrderDetail orderDetail : orderDetails) {
                Goods goods = goodsService.findById(orderDetail.getPid());
                orderDetail.setGoods(goods);
            }

        }
        return orderDetails;//***
    }
    @Override
    public List<Order> findAll()  {
        List<Order> orders=   orderDao.findAll();
        for (Order order : orders) {
            Address address= addressDao.selectById(order.getAid());
            order.setAddress(address);
        }
        return orders;
    }
}
